# -*- coding: utf-8 -*-
import scrapy

from XCFScrapy.items import bannerItem

class BannerspiderSpider(scrapy.Spider):
    name = 'bannerspider'
    allowed_domains = ['xiachufang.com']
    start_urls = ['http://www.xiachufang.com/']

    def parse(self, response):
        # 获取首页轮播图
        headline_slider = response.xpath("//div[@class='unslider headline-slider']/ul/li")
        for headline in headline_slider:
            title = headline.xpath("./a[2]/text()").extract_first("")
            img = headline.xpath("./a/img/attribute::src").extract_first("")
            url = headline.xpath("./a/attribute::href").extract_first("")
            author = headline.xpath("./div[@class='stats']/a[2]/text()").extract_first("")

            # 生成实体数据
            item = bannerItem()
            item['title'] = title
            item['img'] = img
            item['url'] = "http://www.xiachufang.com"+url
            item['author'] = author

            yield item


